import { defineStore } from 'pinia'
import { ref } from 'vue'
import { workflowApi } from '@/api'

export const useWorkflowStore = defineStore('workflow', () => {
  const workflows = ref([])
  const currentWorkflow = ref(null)

  // 获取工作流列表
  const getWorkflows = async () => {
    const response = await workflowApi.getWorkflows()
    workflows.value = response.data
    return response.data
  }

  // 获取工作流详情
  const getWorkflow = async (id: string) => {
    const response = await workflowApi.getWorkflow(id)
    currentWorkflow.value = response.data
    return response.data
  }

  // 创建工作流
  const createWorkflow = async (data: any) => {
    const response = await workflowApi.createWorkflow(data)
    return response.data
  }

  // 更新工作流
  const updateWorkflow = async (id: string, data: any) => {
    const response = await workflowApi.updateWorkflow(id, data)
    return response.data
  }

  // 删除工作流
  const deleteWorkflow = async (id: string) => {
    const response = await workflowApi.deleteWorkflow(id)
    return response.data
  }

  // 执行工作流
  const executeWorkflow = async (id: string, params: any) => {
    const response = await workflowApi.executeWorkflow(id, params)
    return response.data.task_id
  }

  // 获取执行状态
  const getExecuteStatus = async (taskId: string) => {
    const response = await workflowApi.getExecuteStatus(taskId)
    return response.data
  }

  // 获取执行结果
  const getExecuteResult = async (taskId: string) => {
    const response = await workflowApi.getExecuteResult(taskId)
    return response.data
  }

  return {
    workflows,
    currentWorkflow,
    getWorkflows,
    getWorkflow,
    createWorkflow,
    updateWorkflow,
    deleteWorkflow,
    executeWorkflow,
    getExecuteStatus,
    getExecuteResult
  }
}) 